<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Product extends F_Controller {

	function __construct(){
		parent::__construct();
		$data = array(
			'seo_title' => '产品中心',
			'seo_keywords' => '产品展示,合作,购买下单,西安地区,木门,实木门',
			'seo_description' => '优质的产品任凭订购,款式众多,可供挑选'
		);
		$this->load->vars($data);
		$this->navigation['activeController'] = 'product';
		$this->initialize();
	}
	
	function index($page = 1){
		$product = json_decode(file_get_contents(APPPATH . 'data/product.json'));
		$page = is_numeric($page) ? $page : 1;
		$page_size = 16;
		$count = count($product);
		$page_number = ceil($count / $page_size);

		$list = array_slice($product,($page - 1) * $page_size, $page_size);

		$data = array(
			'list' => $list,
			'page' => $this->_init_page($page,$page_number)
		);
		$this->load->view('product', $data);
	}

	function _init(){
		$dir = 'static/images/pro';
		$hander = opendir($dir);
		$data = array();
		while (($file = readdir($hander)) !== false) {
			if($file != '.' && $file != '..'){
				$f = explode('.', $file);
				$d = array(
					'number' => $f[0],
					'path' => $dir .'/'. $file
				);
				$data[] = $d;
			}
			
		}
		file_put_contents('product', json_encode($data));
	}

	function _init_page($current,$count){
		$first = '<a href="'.site_url('product/index').'/1">首页</a>';
		$last = '<a href="'.site_url('product/index').'/'.$count.'">尾页</a>';
		$prev = $current - 1 < 1 ? '' : '<a href="'.site_url('product/index').'/'.($current - 1).'">上一页</a>';
		$next = $current + 1 > $count ? '' : '<a href="'.site_url('product/index').'/'.($current + 1).'">下一页</a>';
		$list = '';
		$star = $current - 1 < 1 ? 1 : $current - 1;
		$end = $star + 3 > $count ? $count : $star + 3;
		//echo $current;
		for($i = $star; $i <= $end; $i++){
			$class = '';
			if($i == $current){
				$class = 'active';
			}
			$list .= '<a class="'.$class.'" href="'.site_url('product/index').'/'.$i.'">'.$i.'</a>';
		}
		return $first . $prev .$list. $next . $last;
	}
	
}
